require "Common/define"
require "Logic/Config"
require "View/HomeUI/UINotice"

UIShop = {};
HelperFunc.SetUIEnv(UIShop)
local this = UIShop;

function UIShop.InitData()
    --this.aniPos = nil;
    --this.dialogText = nil;
    --this.refreshTime = nil;
    --this.content = nil;
    --
    --this.btn_daily = nil;
    --this.btn_honor = nil;
    --this.btn_compete = nil;
    --this.btn_hero = nil;
    --this.btn_discount = nil;
    --this.btn_transcript = nil;
    --this.btn_longjourney = nil;
    --this.btn_mythic = nil
    --this.btn_rank = nil
    --this.img_Icon = nil
    --this.frame = nil
    --
    --this.btn_return = nil;
    --this.btn_refresh = nil;
    --this.refreshImage = nil;
    --this.refreshText = nil;
    --this.newTime = nil;
    this.shopRefreshTime = nil;

    this.shopType = EnumConst.ShopType
    --this.limitOpenType = {
    --    playerLimit = 1,
    --    mapLimit = 2,
    --    unionLimit = 6,
    --}
    this.curShopType = shopType.dailyShop;
    --this.tryOpenShopType = this.curShopType
    --this.shopItemList = {};
    --this.curShopData = {};
    --this.tabGold = {};
    this.stayTime = 5; --秒
    --
    this.consumeType = nil;
    --this.consumeNum = nil;
    --this.backFunc = nil;
    --this.nameHead = nil
    --this.levelHead = nil
    --this.headMenu = nil
    --this.head = nil
    --this.cashObj = nil
    --this.DiamondObj = nil
    --this.CoinObj = nil
    --this.exp = nil
    --this.text_power = nil
    --this.titleList = nil
    --this.costIdList = {};
    --this.btn_SoloBattle = nil;
    --this.btn_Arena = nil;
    this.isUpdateData = false;
end

function UIShop.Init(shopType, func,callbackBGM)
    this.InitData()
    if callbackBGM == nil then
        this.callbackBGM = 2
    else
        this.callbackBGM = callbackBGM
    end
    backFunc = func;
    this.tryOpenShopType = tonumber(shopType or this.curShopType);
    local name, packageName = getUIName(PanelResNames.UIShop);
    panelMgr:Push(name, packageName, this.onCreate, Config.useLocalUI);
end

function UIShop.onCreate(obj)
    if not this.hasInit then
        this.hasInit = true
        aniPos = obj:GetChild("aniPos");
        this.dialogText = obj:GetChild("dialogText");
        headMenu = obj:GetChild("headmenu");
        nameHead = headMenu:GetChild("Name");
        levelHead = headMenu:GetChild("Level");
        head = headMenu:GetChild("Head");
        img_Icon = head:GetChild("head"):GetChild("HeadIcon");
        this.frame = head:GetChild("frame");
        cashObj = headMenu:GetChild("CashLb");
        CoinObj = headMenu:GetChild("CoinLb");
        cashObj:GetController("add").selectedIndex = 0;
        CoinObj:GetController("add").selectedIndex = 0;
        exp = headMenu:GetChild("exp")
        text_power = headMenu:GetChild("text_power");
        if not this.noticePanel then
            this.noticePanel = UINotice.New()
            this.noticePanel:OnCreate(headMenu:GetChild("HorseRaceLamp"))
        end
        this.tabGold = {};
        table.insert(tabGold, CoinObj);
        table.insert(tabGold, cashObj);
        for i = 1, #tabGold do
            tabGold[i].visible = false;
        end
        refreshTime = obj:GetChild("time");
        content = obj:GetChild("content");
        content:SetVirtual();
        this.titleList = obj:GetChild("titleList");
        this.titleList.itemRenderer = this.setTitleData;
        --btn_daily = titleList:GetChild("btn_daily");
        --btn_honor = titleList:GetChild("btn_honor");
        --btn_compete = titleList:GetChild("btn_compete");
        --btn_hero = titleList:GetChild("btn_hero");
        --btn_discount = titleList:GetChild("btn_discount");
        --btn_transcript = titleList:GetChild("btn_transcript");
        --btn_longjourney = titleList:GetChild("btn_longjourney")
        --btn_mythic = titleList:GetChild("btn_Mythic");
        --btn_rank = titleList:GetChild("btn_RankBattle");
        --btn_ChaosTest = titleList:GetChild("btn_ChaosTest");--风暴商店
        --btn_Arena = titleList:GetChild("btn_Arena");--竞技场商店
        --btn_SoloBattle = titleList:GetChild("btn_SoloBattle");--深渊试炼商店
        --btn_Pet = titleList:GetChild("btn_Pet");--飞飞商店
        btn_return = obj:GetChild("btn_return");
        btn_refresh = obj:GetChild("btn_refresh");
        refreshImage = obj:GetChild("refreshImage");
        refreshText = obj:GetChild("refreshText");
        this.sourceUIobj = obj:GetChild("UISourceMultiple");
        this.showSourceController = obj:GetController("showSource")
        this.heroController = obj:GetController("hero")
        this.Btn_Landlady = obj:GetChild("Btn_Landlady")
        this.Effect_LandladyWrap =  obj:GetChild("Effect_Landlady").wrapTarget
        dialogCtrl = obj:GetController("dialogCtrl");
        dialogCtrl.selectedPage = "showDialog";
        timeCtrl = obj:GetController("timeCtrl");
        refreshCtrl = obj:GetController("refreshCtrl");
        refreshCtrl.selectedPage = "notShow";

        --this.btnListByType = {};
        --this.btnListByType[shopType.dailyShop] = btn_daily;
        --this.btnListByType[shopType.honorShop] = btn_honor;
        --this.btnListByType[shopType.competeShop] = btn_compete;
        --this.btnListByType[shopType.heroShop] = btn_hero;
        --this.btnListByType[shopType.discountShop] = btn_discount;
        --this.btnListByType[shopType.transcriptShop] = btn_transcript;
        --this.btnListByType[shopType.mythicShop] = btn_mythic;
        --this.btnListByType[shopType.expeditionShop] = btn_longjourney;
        --this.btnListByType[shopType.rankShop] = btn_rank
        --this.btnListByType[shopType.trialShop] = btn_ChaosTest
        --this.btnListByType[shopType.arenaShop] = btn_Arena
        --this.btnListByType[shopType.realManShop] = btn_SoloBattle;
        --this.btnListByType[shopType.petShop] = btn_Pet;

        --this.funcOpenByType = {};
        --this.funcOpenByType[shopType.dailyShop] = Config.FuncOpenType.DailyShop;
        --this.funcOpenByType[shopType.honorShop] = Config.FuncOpenType.EquipShop;
        --this.funcOpenByType[shopType.heroShop] = Config.FuncOpenType.HeroShop;
        --this.funcOpenByType[shopType.mythicShop] = Config.FuncOpenType.MythicShop;
        --this.funcOpenByType[shopType.expeditionShop] = Config.FuncOpenType.LongJourneyShop;
        --this.funcOpenByType[shopType.rankShop] = Config.FuncOpenType.rankShop;
        --this.funcOpenByType[shopType.trialShop] = Config.FuncOpenType.TrialShop
        --this.funcOpenByType[shopType.arenaShop] = Config.FuncOpenType.ArenaShop
        --this.funcOpenByType[shopType.realManShop] = Config.FuncOpenType.RealManShop;
        --this.funcOpenByType[shopType.petShop] = Config.FuncOpenType.PetShop;
        this.addClick();
        --for k, v in pairs(this.shopType) do
        --    if this.btnListByType[v] and this.funcOpenByType[v] then
        --        logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UIShop, this.funcOpenByType[v], this.btnListByType[v].name, this.btnListByType[v]);
        --    end
        --end
    end
    this.curShopType = this.tryOpenShopType or this.curShopType
    this.refreshHead()
    if this.timerRecycle == nil then
        this.timerRecycle = utimer.timeCall(this.update, 1000, -1)
    end
    this.LocalListenEvent();
    this.initShop();
    if logicMgr.GuideMgr.HasGuidingInCurStage(39) then
        logicMgr.GuideMgr.GuideShowFullscreenMask()
    end
end

function UIShop.OnEnable()
    --商城界面背景音乐
    UIShop.ShopLandladyIdle1Timer()
    --closeUI(PanelResNames.UIHeadMenu);
end

function UIShop.OnDisable()
    if this.timerRecycle then
        utimer.remove(this.timerRecycle)
        this.timerRecycle = nil
    end
    --UpdateBeat:Remove(this.update, slef);
    --coroutine.stop(this.playAni);
    this.LocalCloseEvent();
end

function UIShop.LocalListenEvent()
    ListenEvent(Config.EventType.Fresh_Shop, this.refreshUI);
    ListenEvent(Config.EventType.Fresh_Currency, this.refreshCurShopGold);
    ListenEvent(Config.EventType.Fresh_Bag, this.refreshCurShopGold);
    ListenEvent(Config.EventType.Fresh_Bag,this.updateSource);
    ListenEvent(Config.EventType.Fresh_BagUseItemBack, logicMgr.ItemManager.showGetItem);
end

function UIShop.LocalCloseEvent()
    CloseEvent(Config.EventType.Fresh_Shop, this.refreshUI);
    CloseEvent(Config.EventType.Fresh_Currency, this.refreshCurShopGold);
    CloseEvent(Config.EventType.Fresh_Bag, this.refreshCurShopGold);
    CloseEvent(Config.EventType.Fresh_Bag,this.updateSource);
    CloseEvent(Config.EventType.Fresh_BagUseItemBack, logicMgr.ItemManager.showGetItem);
end

function UIShop.OnDestroy()
    this.hasInit = nil
    if this.noticePanel then
        this.noticePanel:OnDestroy()
        this.noticePanel = nil
    end
    this.removeClick()
    logicMgr.FuncOpenManager.removeStatusCtr(PanelNames.UIShop);
    tabGold = {};
end

function UIShop.addClick()
    --for k, v in pairs(this.shopType) do
    --    if this.btnListByType[v] then
    --        this.btnListByType[v].onClick:Add(function ()
    --            this.onClickBtn(v);
    --        end)
    --    end
    --end
    --btn_daily.onClick:Add(this.onClickDaily)
    --btn_honor.onClick:Add(this.onClickHonor)
    --btn_compete.onClick:Add(this.onClickCompete)
    --btn_longjourney.onClick:Add(this.onClickLongJourney)
    --btn_mythic.onClick:Add(this.onClickMythicShop)
    --btn_hero.onClick:Add(this.onClickHero)
    --btn_discount.onClick:Add(this.onClickDiscount)
    --btn_transcript.onClick:Add(this.onClickTranscript)
    --btn_rank.onClick:Add(this.onClickRank)
    --btn_ChaosTest.onClick:Add(this.onClickTrial)
    --btn_Arena.onClick:Add(this.onClickArena)
    --btn_SoloBattle.onClick:Add(this.onClickSoloBattle)
    btn_return.onClick:Add(this.onClickBack)
    btn_refresh.onClick:Add(this.onClickRefresh)
    this.Btn_Landlady.onClick:Add(this.onClickLandlady)
    for i = 1, #tabGold do
        tabGold[i]:GetChild("btnJump").onClick:Add(function ()
            this.onClickGetCost(i);
        end);
    end
    this.titleList.onClickItem:Add(this.onClickBtn);
    content.scrollPane.onScrollEnd:Add(function ()
        if curShopType == shopType.heroShop then
            content.numItems = #shopItemList
        end
    end);
end

function UIShop.removeClick()
    --for k, v in pairs(this.shopType) do
    --    if this.btnListByType[v] then
    --        this.btnListByType[v].onClick:Clear();
    --    end
    --end
    --btn_daily.onClick:Remove(this.onClickDaily)
    --btn_honor.onClick:Remove(this.onClickHonor)
    --btn_compete.onClick:Remove(this.onClickCompete)
    --btn_hero.onClick:Remove(this.onClickHero)
    --btn_discount.onClick:Remove(this.onClickDiscount)
    --btn_transcript.onClick:Remove(this.onClickTranscript)
    --btn_longjourney.onClick:Remove(this.onClickLongJourney)
    --btn_mythic.onClick:Remove(this.onClickMythicShop)
    --btn_rank.onClick:Remove(this.onClickRank)
    --btn_ChaosTest.onClick:Remove(this.onClickTrial)
    --btn_Arena.onClick:Clear()
    --btn_SoloBattle.onClick:Clear()

    btn_refresh.onClick:Remove(this.onClickRefresh)
    btn_return.onClick:Remove(this.onClickBack)
    this.Btn_Landlady.onClick:Remove(this.onClickLandlady)
    for i = 1, #tabGold do
        tabGold[i]:GetChild("add").onClick:Clear();
    end
    this.titleList.onClickItem:Clear();
end

function UIShop.update()
    --看板娘
    local curTime = HelperFunc.GetServerTime();
    if shopRefreshTime ~= nil and tonumber(shopRefreshTime) ~= 0 then
        timeCtrl.selectedPage = "showTime";
        local timeOffset = (shopRefreshTime / 1000) - curTime
        if timeOffset <= 0 then
            this.updateShopData();
            timeOffset = 0;
        end
        local showTime = "";
        if timeOffset > 86400 then
            showTime = timeToTimeLeft(timeOffset, nil, true);
        else
            showTime = timeToTimeFormat(timeOffset);
        end
        refreshTime.text = "商店刷新时间：" .. showTime;
    else
        timeCtrl.selectedPage = "notShow";
    end
end

--控制商店是否开放
function UIShop.initShop()
    logicMgr.ShopManager.ShopLandladyStatus = logicMgr.ShopManager.LandladyStatus.JOIN_SHOP
    this.shopDataList = logicMgr.ShopManager.returnShopList();
    table.sort(this.shopDataList, function (a, b)
        return a.f_Sort < b.f_Sort;
    end);
    logicMgr.FuncOpenManager.removeStatusCtr(PanelNames.UIShop);
    this.lastTitleObj = nil;
    this.titleList.numItems = #this.shopDataList;
    if this.lastTitleObj == nil then
        for k, v in pairs(this.shopDataList) do
            if logicMgr.FuncOpenManager.isOpen(v.f_FunctionId) then
                this.lastTitleObj = this.titleList:GetChildAt(k-1);
                this.lastTitleObj.selected = true;
                this.titleList:ScrollToView(0);
                this.curShopType = v.f_ShopID;
                break;
            end
        end
    end
    if this.lastTitleObj ~= nil then
        this.updateShopData();
    end
    --local openLimitList = {};
    --openLimitList[this.limitOpenType.playerLimit] = logicMgr.HeroManager.GetRoleLevel();
    --openLimitList[this.limitOpenType.mapLimit] = logicMgr.MapManager.getMapId();
    --openLimitList[this.limitOpenType.unionLimit] = logicMgr.GuildManager.getUnionLv();
    --local dataTable = G.dataTable["t_shophead"];
    --for k, v in pairs(this.btnListByType) do
    --    v.visible = false;
    --    v.selected = false;
    --end
    --for k, v in pairs(dataTable) do
    --    local limitCondition = decodeJsonStrToTabel(v.f_UnlockCondition);
    --    local limitType = limitCondition[1][1];
    --    local limitLevel = limitCondition[1][2];
    --    if openLimitList[limitType] ~= nil and openLimitList[limitType] >= limitLevel and this.btnListByType[v.f_ShopID] then
    --        this.btnListByType[v.f_ShopID].visible = true;
    --    end
    --end
    --if this.btnListByType[this.curShopType] and this.btnListByType[this.curShopType].visible then
    --    local index = titleList:GetChildIndex(this.btnListByType[this.curShopType]);
    --    titleList:ScrollToView(index);
    --    this.btnListByType[this.curShopType].onClick:Call();
    --else
    --    this.onClickBack();
    --    UISysTips.AddMsg(getLanguage("ErrorCode_1301"));
    --end
end

function UIShop.setTitleData(index, obj)
    local tableData = this.shopDataList[index+1];
    obj.icon = "ui://UIShop/" .. tableData.f_Icon;
    obj.title = getLanguage(tableData.f_Name);
    logicMgr.FuncOpenManager.addStatusCtr(PanelNames.UIShop, tableData.f_FunctionId, obj.title, obj);
    if tableData.f_ShopID == this.curShopType and logicMgr.FuncOpenManager.isOpen(tableData.f_FunctionId) then
        this.lastTitleObj = obj;
        obj.selected = true;
        this.titleList:ScrollToView(index);
    else
        obj.selected = false;
    end
end
function UIShop.onClickBtn(context)
    local go = context.data
    local index = this.titleList:GetChildIndex(go);
    local tableData = this.shopDataList[index+1];
    if not logicMgr.FuncOpenManager.isOpen(tableData.f_FunctionId, true, go) then
        go.selected = false;
        this.lastTitleObj.selected = true;
        return ;
    end
    this.lastTitleObj = go;
    this.curShopType = tableData.f_ShopID;
    this.updateShopData();
end
----每日商店
--function UIShop.onClickDaily()
--    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.DailyShop, true,btn_daily) then
--        this.btnListByType[this.curShopType].onClick:Call();
--        return ;
--    end
--    this.curShopType = shopType.dailyShop;
--    --this.refreshCurShopGold();
--    --shopHandler.reqShopDataList(this.curShopType);
--    this.updateShopData();
--end
--
----荣誉商店
--function UIShop.onClickHonor()
--    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.EquipShop, true,btn_honor) then
--        this.btnListByType[this.curShopType].onClick:Call();
--        return ;
--    end
--    this.curShopType = shopType.honorShop;
--    --this.refreshCurShopGold();
--    --shopHandler.reqShopDataList(this.curShopType);
--    this.updateShopData();
--end
--
----竞技场商店
--function UIShop.onClickCompete()
--    this.curShopType = shopType.competeShop;
--    --this.refreshCurShopGold();
--    --shopHandler.reqShopDataList(this.curShopType);
--    this.updateShopData();
--end
--
----英雄商店
--function UIShop.onClickHero()
--    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.HeroShop, true,btn_hero) then
--        this.btnListByType[this.curShopType].onClick:Call();
--        return ;
--    end
--    this.curShopType = shopType.heroShop;
--    --this.refreshCurShopGold();
--    --shopHandler.reqShopDataList(this.curShopType);
--    this.updateShopData();
--end
--
----折扣商店
--function UIShop.onClickDiscount()
--    this.curShopType = shopType.discountShop;
--    --this.refreshCurShopGold();
--    --shopHandler.reqShopDataList(this.curShopType);
--    this.updateShopData();
--end
--
----副本商店
--function UIShop.onClickTranscript()
--    this.curShopType = shopType.transcriptShop;
--    --this.refreshCurShopGold();
--    --shopHandler.reqShopDataList(this.curShopType);
--    this.updateShopData();
--end
----远征商店
--function UIShop.onClickLongJourney()
--    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.LongJourneyShop, true,btn_longjourney) then
--        this.btnListByType[this.curShopType].onClick:Call();
--        return ;
--    end
--    this.curShopType = shopType.expeditionShop
--    --this.refreshCurShopGold()
--    --shopHandler.reqShopDataList(this.curShopType)
--    this.updateShopData();
--end
----排位商店
--function UIShop.onClickRank()
--    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.rankShop, true) then
--        this.btnListByType[this.curShopType].onClick:Call();
--        return ;
--    end
--    this.curShopType = shopType.rankShop
--    --this.refreshCurShopGold()
--    --shopHandler.reqShopDataList(this.curShopType)
--    this.updateShopData();
--end
--function UIShop.onClickMythicShop()
--    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.MythicShop, true,btn_mythic) then
--        this.btnListByType[this.curShopType].onClick:Call();
--        return ;
--    end
--    this.curShopType = shopType.mythicShop
--    --this.refreshCurShopGold()
--    --shopHandler.reqShopDataList(this.curShopType)
--    this.updateShopData();
--end
----风暴峡湾商店
--function UIShop.onClickTrial()
--    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.TrialShop, true,btn_ChaosTest) then
--        this.btnListByType[this.curShopType].onClick:Call();
--        return ;
--    end
--    this.curShopType = shopType.trialShop;
--    --this.refreshCurShopGold()
--    --shopHandler.reqShopDataList(this.curShopType)
--    this.updateShopData();
--end
--
----竞技场商店
--function UIShop.onClickArena()
--    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.ArenaShop, true,btn_Arena) then
--        this.btnListByType[this.curShopType].onClick:Call();
--        return
--    end
--    this.curShopType = shopType.arenaShop
--    --this.refreshCurShopGold()
--    --shopHandler.reqShopDataList(this.curShopType)
--    this.updateShopData();
--end
--
----深渊试炼
--function UIShop.onClickSoloBattle()
--    if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.RealManShop, true,btn_SoloBattle) then
--        this.btnListByType[this.curShopType].onClick:Call();
--        return
--    end
--    this.curShopType = shopType.realManShop;
--    --this.refreshCurShopGold()
--    --shopHandler.reqShopDataList(this.curShopType)
--    this.updateShopData();
--end

function UIShop.updateShopData()
    if this.isUpdateData then
        return;
    end
    this.isUpdateData = true;
    this.refreshCurShopGold();
    shopHandler.reqShopDataList(this.curShopType);
end

--刷新商店货币
function UIShop.refreshCurShopGold()
    local tabShopGoldData = logicMgr.ShopManager.returnShopGold(this.curShopType);
    for k,v in pairs(tabGold) do
        v.visible = false;
    end
    this.costIdList = {};
    for i = 1, #tabShopGoldData do
        if tabGold[i] == nil then
            break ;
        end
        tabGold[i].visible = true;
        tabGold[i].icon = "ui://ItemAtlas/" .. tabShopGoldData[i]["currencyIcon"];
        if tonumber(tabShopGoldData[i]["currencyId"]) == CurrencyEnum.GOLD then
            tabGold[i].text = logicMgr.ItemManager.getBagNumByTableID(tonumber(tabShopGoldData[i]["currencyId"]))
        else
            tabGold[i].text = getNumString(logicMgr.ItemManager.getBagNumByTableID(tonumber(tabShopGoldData[i]["currencyId"])))
        end
        this.costIdList[i] = tabShopGoldData[i]["currencyIcon"];
    end
end

function UIShop.refreshUI()
    --取数据
    curShopData = {};
    curShopData = logicMgr.ShopManager.returnShopData();
    this.ChangeLandladyStatus()
    if curShopData ~= nil then
        shopItemList = {};
        shopItemList = curShopData["shopItems"];

        content.itemRenderer = this.setData;
        if #shopItemList <= 0 then
            this.heroController.selectedIndex = 1;
        else
            this.heroController.selectedIndex = 0;
            content.numItems = #shopItemList;
        end
        --HelperFunc.ListTranstionPlay(content)
        --dynamicList:setDataByJsonArray(getJsonStr(shopItemList));
    end

    --自动刷新,显示时间
    if curShopData["nextRefreshTime"] ~= nil then
        shopRefreshTime = curShopData["nextRefreshTime"];
    else
        shopRefreshTime = nil;
    end
    this.update();
    --刷新当前商店手动刷新消耗货币类型和数量
    --btn_refresh.gameObject:SetActive(true);
    refreshCtrl.selectedPage = "show";

    local refreshUp = tonumber(curShopData["refreshUp"])
    local refreshTimes = tonumber(curShopData["refreshTimes"])
    local refreshConsume = curShopData["refreshConsume"]

    if refreshUp == 0 then
        refreshCtrl.selectedPage = "notShow";
    else

        ----如果是0的话  默认取1用来初始化显示信息
        --if refreshTimes == 0 then
        --    refreshTimes = 1
        --end

        for i = 1, #refreshConsume do
            local consume = refreshConsume[i]
            if refreshTimes + 1 >= tonumber(consume[1]) then
                consumeType = tostring(consume[2])
                consumeNum = consume[3]
            end
        end
        if consumeType then
            local consumeIcon = G.dataTable["t_item"][consumeType].f_Icon;
            --print("====consumeIcon==========>"..consumeIcon);
            refreshImage.url = EnumConst.AtlasPath.ItemAtlas .. consumeIcon;

        end
        refreshText.text = consumeNum
    end
    this.isUpdateData = false;
end

function UIShop.setData(index, obj)
    local itemValue = shopItemList[index + 1];
    itemValue["shopId"] = curShopData["shopId"];
    itemValue["showCounter"] = index % 4 == 0;

    local item = CtrlManager.GetCtrl(PanelNames.ShopItem);
    if item ~= nil then
        if this.curShopType==shopType.heroShop then
            item.initData(obj, itemValue,true)
        else
            item.initData(obj, itemValue);
        end

    end
end

--手动刷新
function UIShop.onClickRefresh()
    if this.isUpdateData then
        return;
    end
    local refreshUp = tonumber(curShopData["refreshUp"])
    local refreshTimes = tonumber(curShopData["refreshTimes"])
    if refreshTimes >= refreshUp then
        UISysTips.AddMsg(getLanguage("Sys_ShopNoRefreshTimes"));
        return ;
    end
    local hadNum = logicMgr.ItemManager.getBagNumByTableID(tonumber(consumeType))
    local tips = consumeNum
    if tonumber(hadNum) < tonumber(consumeNum) then
        tips = string.format("<color=red>%s</color>", tips)
    end

    UICommonPop.Init(5, function(flag)
        if flag then
            if tonumber(hadNum) >= tonumber(consumeNum) then
                --发送请求刷新商店
                this.isUpdateData = true;
                local shopId = curShopData["shopId"];
                local refreshTimes = curShopData["refreshTimes"];
                shopHandler.reqRefShopData(shopId, refreshTimes);
                local _refreshDic = {}
                _refreshDic.shop_id = tonumber(curShopData["shopId"]);
                -- _refreshDic.refresh_times = tonumber(curShopData["refreshTimes"]) + 1;
                -- _refreshDic.mythic_dungeons_level_id = logicMgr.MythicDungeonsManager.getMaxLayer();
                -- _refreshDic.cost_token_id = consumeType;
                -- _refreshDic.cost_token_number = consumeNum;
                -- _refreshDic.get_consumption_points = logicMgr.ItemManager.GetItemConsumptionPoints(consumeType) * consumeNum;
                logicMgr.ShopManager.setRefreshTAData(_refreshDic);
            else
                UISysTips.AddMsg(getLanguage("Character_Tips7"))
            end
        end
    end, tips)
end

--返回
function UIShop.onClickBack()
    closeUI(PanelNames.UIShop)
    logicMgr.ShopManager.ShopLandladyStatus = logicMgr.ShopManager.LandladyStatus.EXIT_SHOP
    this.ChangeLandladyStatus()
    --showUI(PanelNames.UIHeadMenu);
    if backFunc ~= nil then
        backFunc();
    end
end

--- 刷新头像信息
function UIShop.refreshHead()
    local level = logicMgr.HeroManager.GetRoleLevel()
    nameHead.text = logicMgr.HeroManager.GetRoleInfo().name;
    levelHead.text = level
    img_Icon.url = logicMgr.SkinManager.GetHeroIconBySkinId(logicMgr.HeroManager.GetRoleInfo("pic"))
    local itemid = logicMgr.HeroManager.GetRoleInfo("picFrame")
    this.frame.url = EnumConst.AtlasPath.ICONAtlas .. itemid;
    local curExp = logicMgr.ItemManager.getCurrencyCountByModelId(CurrencyEnum.RoleExp)
    local maxExp = dataTable.getRow("t_characterexp", MakeTopLevelId(level)).f_CharacterExp
    local value = curExp / maxExp
    if value >= 1 then
        value = 1;
    end
    exp.value = value * 100
    text_power.text = getNumString(logicMgr.HeroManager.GetRoleInfo("maxPower"))
end

function UIShop.onClickGetCost(key)
    --showUI(PanelNames.UISource, function ()
    --    this.onClickBack();
    --end, this.costIdList[key]);
    this.showSourceController.selectedIndex = 1;
    this.showSourceList = {this.costIdList[key]}
    this.updateSource();
end

function UIShop.updateSource()
    logicMgr.ItemManager.updateSourceUI(this.sourceUIobj, this.showSourceList, function ()
        this.showSourceController.selectedIndex = 0;
    end, function ()
        this.showSourceController.selectedIndex = 0;
        this.onClickBack();
    end );
end


this.LANDLADY_IDLE2_TIME = 3500
this.LANDLADY_ANIM_INTERVAL = 10000
this.LANDLADY_TIMER_INTERVAL = 500
this.lapseTime = 0

function UIShop.onClickLandlady()
    logicMgr.ShopManager.ShopLandladyStatus = logicMgr.ShopManager.LandladyStatus.CLICK_EFFECT
    this.ChangeLandladyStatus()
end

-- 播放 idle1 动画
function UIShop.ShopLandladyIdle1Timer()
    if this.timerIdle1Id == nil then
        this.timerIdle1Id = utimer.timeCall(function ()
            if logicMgr.ShopManager.ShopLandladyStatus == logicMgr.ShopManager.LandladyStatus.CLICK_EFFECT then
                --播放idle2时，这个timer不做处理
            elseif logicMgr.ShopManager.ShopLandladyStatus ~= logicMgr.ShopManager.LandladyStatus.HOVER_SHOP then
                logicMgr.ShopManager.ShopLandladyStatus = logicMgr.ShopManager.LandladyStatus.HOVER_SHOP
            else
                this.ChangeLandladyStatus()
            end
        end,10000,-1)
    end
end
-- 播放 idle2 动画
function UIShop.ShopLandladyIdel2Timer(animTimeLength)
    if tonumber(animTimeLength) == nil then
        animTimeLength = 3500
    end
    utimer.remove(this.timerIdle2Id)
    this.timerIdle2Id = utimer.timeCall(function ()
        utimer.remove(this.timerIdle2Id)
        this.timerIdle2Id = nil
        Util.PlayAction(this.Effect_LandladyWrap, "idle1", true)
        logicMgr.ShopManager.ShopLandladyStatus = logicMgr.ShopManager.LandladyStatus.SWITCH_PAGE
    end,animTimeLength,1)
end

--看板娘状态变更
-- 读取t_global表，需要配置动画对应的名称和时长
-- ilde2 设置为循环 true，是为了动作看起来 顺畅点
function UIShop.ChangeLandladyStatus()
    if logicMgr.ShopManager.ShopLandladyStatus == logicMgr.ShopManager.LandladyStatus.JOIN_SHOP then
        logicMgr.ShopManager.ShopLandladyStatus = logicMgr.ShopManager.LandladyStatus.SWITCH_PAGE
        AudioManager.Play(5000201)
        this.dialogText.text = logicMgr.ShopManager.returnShopDialogText()
        local _animInfo = decodeJsonStrToTabel(G.dataTable["t_global"]["242"].f_string_value)
        if _animInfo and #_animInfo == 2 then
            Util.PlayAction(this.Effect_LandladyWrap, _animInfo[1], true)
            UIShop.ShopLandladyIdel2Timer(tonumber(_animInfo[2]))
        end
    elseif logicMgr.ShopManager.ShopLandladyStatus == logicMgr.ShopManager.LandladyStatus.BUY_GOODS then
        AudioManager.Play(5000202)
        this.dialogText.text = getLanguage("BossLadyWords_buy")
        local _animInfo = decodeJsonStrToTabel(G.dataTable["t_global"]["243"].f_string_value)
        if _animInfo and #_animInfo == 2 then
            Util.PlayAction(this.Effect_LandladyWrap, _animInfo[1], true)
            UIShop.ShopLandladyIdel2Timer(tonumber(_animInfo[2]))
        end
    elseif logicMgr.ShopManager.ShopLandladyStatus == logicMgr.ShopManager.LandladyStatus.REFRESH_GOODS then
        AudioManager.Play(5000203)
        this.dialogText.text = getLanguage("BossLadyWords_refresh")
        local _animInfo = decodeJsonStrToTabel(G.dataTable["t_global"]["244"].f_string_value)
        if _animInfo and #_animInfo == 2 then
            Util.PlayAction(this.Effect_LandladyWrap, _animInfo[1], true)
            UIShop.ShopLandladyIdel2Timer(tonumber(_animInfo[2]))
        end
    elseif logicMgr.ShopManager.ShopLandladyStatus == logicMgr.ShopManager.LandladyStatus.HOVER_SHOP then
        this.dialogText.text = logicMgr.ShopManager.returnShopDialogText()
        local _animInfo = decodeJsonStrToTabel(G.dataTable["t_global"]["245"].f_string_value)
        if _animInfo and #_animInfo == 2 then
            Util.PlayAction(this.Effect_LandladyWrap, _animInfo[1], true)
            UIShop.ShopLandladyIdel2Timer(tonumber(_animInfo[2]))
        end
    elseif logicMgr.ShopManager.ShopLandladyStatus == logicMgr.ShopManager.LandladyStatus.SWITCH_PAGE then
        this.dialogText.text = logicMgr.ShopManager.returnShopDialogText()
        local _animInfo = decodeJsonStrToTabel(G.dataTable["t_global"]["245"].f_string_value)
        if _animInfo and #_animInfo == 2 then
            Util.PlayAction(this.Effect_LandladyWrap, _animInfo[1], true)
            UIShop.ShopLandladyIdel2Timer(tonumber(_animInfo[2]))
        end
    elseif logicMgr.ShopManager.ShopLandladyStatus == logicMgr.ShopManager.LandladyStatus.CLICK_EFFECT then
        -- AudioManager.Play(5000205)
        this.dialogText.text = logicMgr.ShopManager.returnShopDialogText()
        local _animInfo = decodeJsonStrToTabel(G.dataTable["t_global"]["246"].f_string_value)
        if _animInfo and #_animInfo == 2 then
            Util.PlayAction(this.Effect_LandladyWrap, _animInfo[1], true)
            UIShop.ShopLandladyIdel2Timer(tonumber(_animInfo[2]))
        end
    elseif logicMgr.ShopManager.ShopLandladyStatus == logicMgr.ShopManager.LandladyStatus.EXIT_SHOP then
        AudioManager.Play(5000204)
        utimer.remove(this.timerIdle1Id)
        this.timerIdle1Id = nil
    else
        utimer.remove(this.timerIdle1Id)
        this.timerIdle1Id = nil
    end
end

return UIShop;